package com.ftqh.tutorial.service;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Administrator on 2017/5/5.
 */
@Service
public class ComputeService {
    @Autowired
    RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod="addFallback")
    public String add(){
        return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
    }
    public String addFallback(){
        return "add service is not available";
    }
    @HystrixCommand(fallbackMethod="getUserFallback", commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "5000")})
    public String getUser(Integer id){
        ResponseEntity<JSONObject> jsonObject = restTemplate.getForEntity("http://COMPUTE-SERVICE/users/"+id, JSONObject.class);
        return jsonObject.getBody().toJSONString();
    }
    public String getUserFallback(Integer id){
        return "get user service is not available, param[id:"+id+"]";
    }
}
